package util.arff;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NewStructureArffGenerator extends AbstractArffGenerator{

	private boolean isImp;
	private boolean includeEnergy;
	private final static String separator = System.getProperty("file.separator");
	public NewStructureArffGenerator(String structPath, String resultPath, boolean considerZeros, boolean includeEnergy) {
		this.structPath = structPath;
		this.resultPath = resultPath;
		this.includeEnergy = includeEnergy;
		this.considerZeros = considerZeros;
		try {
			this.proteins = createProteinList(resultPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private List<String> createProteinList(String path) throws IOException {
		List<String> result = new ArrayList<String>();
		File file = new File(path);
		if (file.isDirectory()) {
			String[] filePathField = file.getAbsolutePath().split(separator);
			String type = filePathField[filePathField.length - 1];
			isImp = type.contains("imp");
			for (int i = 0; i < file.list().length; i++) {
				result.add(file.list()[i]);
			}
		}
		return result;
	}

	@Override
	public void generateArff() {
		try {
			if (isImp) {
				for (String singleProtein : proteins) {
					String path = structPath + separator + singleProtein + ".fasta";
					BufferedReader reader = new BufferedReader(new FileReader(path));
					String head = reader.readLine();
					
					String sequence = reader.readLine();
					
					reader.readLine();
					String line = reader.readLine();
					
					//System.out.println(head + "\n" + sequence);	Was here only for testing purposes
					
					String arffAbsolutePath = resultPath + separator + singleProtein + separator + "prot.arff";
					super.createArffFileForIMP(line.toCharArray(),sequence.toCharArray(), arffAbsolutePath, includeEnergy);
					reader.close();
				}
			} else {
				for (String singleProtein : proteins) {
					String arffAbsolutePath = resultPath + separator + singleProtein + separator + "prot.arff";
					String solQueryInPath = resultPath + separator + singleProtein + separator + "query.in";
					super.createArffFileForSOL(singleProtein, arffAbsolutePath, solQueryInPath, includeEnergy);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	public boolean isIncludeEnergy() {
	    return includeEnergy;
	}

	public void setIncludeEnergy(boolean includeEnergy) {
	    this.includeEnergy = includeEnergy;
	}
	
	public void setStructPath(String path) {
	    this.structPath = path;
	}
	
	public String getStructPath() {
	    return this.structPath;
	}
	
	public void setResultPath(String path) throws IOException {
	    this.resultPath = path;
	    this.proteins = createProteinList(resultPath);
	}
	
	public String getResultPath() {
	    return this.resultPath;
	}
}
